package com.java.common.base.action;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.opensymphony.xwork2.ActionSupport;

public class BaseAction extends ActionSupport {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -1369234551947308075L;

	protected String getWebApplicationAbsolutePath() {
        String realPath = getServletContext().getRealPath("/");
        if (realPath != null) {
            if (!realPath.endsWith("/"))
                return realPath + "/";
        }
        return realPath;
    }

    protected ServletContext getServletContext() {
        return ServletActionContext.getServletContext();
    }

    protected HttpServletRequest getRequest() {
        return ServletActionContext.getRequest();
    }

    protected HttpSession getHttpSession() {
        return ServletActionContext.getRequest().getSession();
    }

    protected HttpServletResponse getResponse() {
        return ServletActionContext.getResponse();
    }

    public Object getBean(String name) {
        ServletContext servletContext = ServletActionContext.getServletContext();
        WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
        return wac.getBean(name);
    }
}
